package service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class CloseConnections {

  public static void closeAll(ResultSet rs, PreparedStatement statement, Connection connection) {
    closeResultSet(rs);
    closeStatement(statement);
    closeConnection(connection);
  }

  private static void closeResultSet(ResultSet rs) {
    if (rs != null) {
      try {
        rs.close();
      } catch (Exception e) {
        System.out.println("Exception occured: " + e);
      }
      rs = null;
    }
  }

  private static void closeStatement(PreparedStatement statement) {
    if (statement != null) {
      try {
        statement.close();
      } catch (Exception e) {
        System.out.println("Exception occured: " + e);
      }
      statement = null;
    }
  }

  private static void closeConnection(Connection connection) {
    if (connection != null) {
      try {
        connection.close();
      } catch (Exception e) {
        System.out.println("Exception occured: " + e);
      }
      connection = null;
    }
  }
}
